package com.hospital.doctor.doctory.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.hospital.doctor.doctorg.entity.QueueEntity;
import com.hospital.doctor.doctory.entity.DepartmentCt;
import com.hospital.doctor.doctory.entity.DoctorDiagnose;
import com.hospital.doctor.doctory.entity.DrugVO;
import com.hospital.doctor.doctory.entity.PatientDiagnosisResults;
import com.hospital.doctor.doctory.service.DepartmentCtService;
import com.hospital.doctor.doctory.service.DoctorMedicineService;
import com.hospital.doctor.doctory.service.PatientDiagnosisResultsService;
import com.hospital.doctor.doctory.service.QueueService;
import com.hospital.frontdesk.fronxiang.entity.DoctorQueue;
import com.hospital.frontdesk.fronxiang.entity.FrontdeskCard;
import com.hospital.frontdesk.fronxiang.service.FrontdeskCardService;
import com.hospital.user.registration.entity.Doctor_queue;
import com.hospital.xiaochengxiu.yan.entity.DepartmentQueue;
import com.hospital.xiaochengxiu.yan.entity.DoctorMedicine;
import com.hospital.yang.login.entity.DoctorLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.boot.context.properties.bind.Name;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;

/**
 * @Author ：ysb
 * @ClassName ：DoctorImproveController
 * @Package ：com.hospital.doctor.doctory.controller
 * @Description:
 * @Date:2021/11/2 8:29
 */
@Controller
@RequestMapping("/doctory")
public class DoctorImproveController {

    @Autowired
   private QueueService queueService;
    @Autowired
    private FrontdeskCardService frontdeskCardService;
    @Autowired
    private DepartmentCtService departmentCtService;
    @Autowired
    private DoctorMedicineService doctorMedicineService;
    @Autowired
    private PatientDiagnosisResultsService patientDiagnosisResultsService;

    //进入医生改进页面并查询患者列表
    @RequestMapping("/toDoctorImprove")
    public String toDoctorImprove( Integer doctorId, @RequestParam(defaultValue = "0") Integer state, Model model){
        SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
        sdf.applyPattern("yyyy-MM-dd");// a为am/pm的标记
        Date date = new Date();// 获取当前时间
        List<QueueEntity> list = queueService.getList(doctorId, state,sdf.format(date));
        model.addAttribute("userQueueList",list);
//        List<QueueEntity> list1 = queueService.getList(doctorId, 2,null);
//        model.addAttribute("userQueueList2",list1);
        return "doctor/doctorImprove";
    }

    //查询已诊断的患者列表
    @RequestMapping("/selectDoctorImproveList")
    @ResponseBody
    public Object selectDoctorImprove(HttpServletRequest request){
        DoctorLogin doctor=(DoctorLogin)request.getSession().getAttribute("doctor");
        String doctorId = doctor.getDoctorId();
        return JSON.toJSON(queueService.getList(Integer.parseInt(doctorId), 2,null));
    }

     /**
     * 查询患者列表
     * @return
     *//*
    @RequestMapping("/selectQueue")
    @ResponseBody
    public String selectQueue(Integer doctorId, @RequestParam(defaultValue = "0") Integer state){
        List<QueueEntity> list = queueService.getList(doctorId, state);
        return JSON.toJSONString(list);
    }*/

    /**
     * 查询用户信息
     * @return
     */
    @RequestMapping("/selectUserInfo")
    @ResponseBody
    public Object selectUserInfo(Integer patientId){
        FrontdeskCard frontdeskCard = frontdeskCardService.selectById(patientId);
        return JSON.toJSON(frontdeskCard);
    }

    /**
     * 查询历史病历
     * @return
     */
    @RequestMapping("/selectMedical")
    @ResponseBody
    public Object selectMedical(Integer patientId){
        List<DoctorDiagnose> doctorDiagnoses = queueService.selectDiagnose(patientId);
        System.out.println(doctorDiagnoses.toString());
        return JSON.toJSON(doctorDiagnoses);
    }

    /**
     * 根据用户诊疗卡号查询出用户的检查记录
     * @param CdiagnoseNO
     * @return
     */
    @RequestMapping("/selectInspectionRecord")
    @ResponseBody
    public Object selectInspectionRecord(Integer CdiagnoseNO){
        if(CdiagnoseNO==null || CdiagnoseNO.equals("")){
            return null;
        }
        List<DepartmentCt> departmentCts = departmentCtService.selectInspectionRecord(CdiagnoseNO);
        return JSON.toJSON(departmentCts);
    }

    /**
     * 开检查单
     * @param
     * @return
     */
    @RequestMapping("/InspectionRecord")
    @ResponseBody
    public String insertRecord(String userid,String userTime,String uderType,Double userMeney,Integer state,String usercheck){
        if(uderType.equals("CT")){
            userMeney=300.00;
        }else if(uderType.equals("核磁共振")){
            userMeney=300.00;
        }else if(uderType.equals("B超")){
            userMeney=150.00;
        }else {
            userMeney=250.00;
        }
        DepartmentQueue queue=new DepartmentQueue(Integer.parseInt(userid),userTime,uderType,usercheck,userMeney,state);
        System.out.println(queue.toString());
        return JSON.toJSONString(departmentCtService.insertRecord(queue));
    }


    /**
     * 查询全部药品
     * @return
     */
    @RequestMapping("/selectAllDrug")
    @ResponseBody
    public Object selectAllDrug(){
        return JSON.toJSON(queueService.selectAll());
    }


    /**
     * 开药
     * 修改状态，增加到开药yy记录表中
     * @return
     */
    @RequestMapping("/prescribe")
    @ResponseBody
    public String prescribe(HttpServletRequest request,String patientId, String time,String patientName,String list,String time2,String patientDiagnosisResults,String textarea){
        List<DrugVO> list1 = JSONArray.parseArray(list,DrugVO.class);
        List<DoctorMedicine> list2=new ArrayList<>();
        DoctorLogin doctor=(DoctorLogin)request.getSession().getAttribute("doctor");
        list1.forEach(s->{
            list2.add(new DoctorMedicine(Integer.parseInt(patientId),patientName,
                    Integer.parseInt(s.getDrugid()),s.getSum(),time2,Integer.parseInt(doctor.getDoctorId()),s.getYongfang(),0
                    ));
        });

        //插入诊断记录表
        System.err.println("时间2当前时间："+time2);
        System.err.println("时间1挂号时间："+time);
        System.err.println("医嘱："+textarea);
        System.err.println("诊断结果："+patientDiagnosisResults);

        Doctor_queue queue=new Doctor_queue();
        queue.setTime(time);
        queue.setPatientId(patientId);
        //修改状态及插入记录
        PatientDiagnosisResults patientDiagnosisResults1=new PatientDiagnosisResults(Integer.parseInt(patientId),patientName,Integer.parseInt(doctor.getDoctorId()),time,time2,patientDiagnosisResults,textarea);
        DoctorDiagnose doctorDiagnose=new DoctorDiagnose(Integer.parseInt(patientId),patientDiagnosisResults,time2,Integer.parseInt(doctor.getDoctorId()));
        doctorMedicineService.prescribe(queue,list2,patientDiagnosisResults1,doctorDiagnose);
        return JSON.toJSONString("true");
    }


    /**
     * 查询用户的诊断结果
     * @return
     */
    @RequestMapping("/selectRecords")
    @ResponseBody
    public Object selectRecords(HttpServletRequest request){
        DoctorLogin doctor=(DoctorLogin)request.getSession().getAttribute("doctor");
        String doctorId = doctor.getDoctorId();
        return JSON.toJSON(patientDiagnosisResultsService.selectByPatientId(doctorId));
    }

}
